<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试时间">
        <a-input placeholder="考试时间" v-decorator="['studentId', {rules: [{required: true, message: '请输入考试时间'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总得分">
        <a-input placeholder="总得分" v-decorator="['mark', {rules: [{required: true, message: '请输入总得分'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总得分">
        <a-input placeholder="总得分" v-decorator="['testPaperInfoId', {rules: [{required: true, message: '请输入总得分'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="1 已批改 0.待批改">
        <a-input placeholder="1 已批改 0.待批改" v-decorator="['correctFlag', {rules: [{required: true, message: '请输入1 已批改 0.待批改'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="系统判分">
        <a-input placeholder="系统判分" v-decorator="['systemMark', {rules: [{required: true, message: '请输入系统判分'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="老师评分">
        <a-input placeholder="老师评分" v-decorator="['teacherMark', {rules: [{required: true, message: '请输入老师评分'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试耗时">
        <a-input placeholder="考试耗时" v-decorator="['examTime', {rules: [{required: true, message: '请输入考试耗时'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="批改形式">
        <a-select placeholder="请选择批改形式" v-decorator="['correctType']" allowClear>
          <a-select-option v-for="(b, index) in correctTypeList" :key="index" :value="b.dictValue">{{ b.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="批改人">
        <a-input placeholder="批改人" v-decorator="['adminId', {rules: [{required: true, message: '请输入批改人'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客观题答对题数">
        <a-input placeholder="客观题答对题数" v-decorator="['rightNumber', {rules: [{required: true, message: '请输入客观题答对题数'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客观题答错题数">
        <a-input placeholder="客观题答错题数" v-decorator="['errorNumber', {rules: [{required: true, message: '请输入客观题答错题数'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="主观题数量">
        <a-input placeholder="主观题数量" v-decorator="['subjectiveQuestionNumber', {rules: [{required: true, message: '请输入主观题数量'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="试题数量">
        <a-input placeholder="试题数量" v-decorator="['questionNumber']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试时间">
        <a-input placeholder="考试时间" v-decorator="['examTimeLongValue', {rules: [{required: true, message: '请输入考试时间'}]}]" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveEExamInfo } from '@/api/eExamInfo'
import pick from 'lodash.pick'

export default {
  name: 'EExamInfoModal',
  props: {
    correctTypeList: {
      type: Array,
      required: true
    }
  },
  components: {},
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
  },
  methods: {
    add () {
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit (record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'studentId', 'createDate', 'updateDate', 'mark', 'testPaperInfoId', 'correctFlag', 'systemMark', 'teacherMark', 'examTime', 'correctType', 'adminId', 'rightNumber', 'errorNumber', 'subjectiveQuestionNumber', 'questionNumber', 'examTimeLongValue'))
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveEExamInfo(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
